﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace JobList
{
    public class Singleton
    {
        private Singleton() { }
        public static Singleton _instance
        {
            get
            {
                if (instance == null)
                {
                    lock (SingletonLock)
                    {
                        instance = new Singleton();
                    }
                }
                return instance;
            }
        }

        private static Singleton instance = null;

        private static object SingletonLock = new object();//加锁，防止多线程
        public string FilePath { get; set; }
        public string ConfigIniPath { get; set; }
        public string JobListJsonPath { get; set; }
        public string JobListContentPath { get; set; }

        public List<CJobList> cJobLists = new List<CJobList>();

        public string ActiveCJobListID { get; set; }

        public int FChangeJobListShowValue { get; set; }

        public void IsExistDirPath(string DirPath)
        {
            if (!Directory.Exists(DirPath))
            {
                Directory.CreateDirectory(DirPath);
            }
        }

        public void SavecJobLists()
        {
            string ScJobLists = JsonConvert.SerializeObject(cJobLists);
            ScJobLists = JsonTree(ScJobLists);
            using (FileStream _file = new FileStream(JobListJsonPath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
            {
                byte[] byData;
                byData = Encoding.UTF8.GetBytes(ScJobLists);
                _file.Seek(0, SeekOrigin.Begin);
                _file.Write(byData, 0, byData.Length);
            }
        }

        #region Json格式化

        /// <summary>
        /// JSON字符串格式化
        /// </summary>
        /// <param name="json"></param>
        /// <returns></returns>
        public string JsonTree(string json)
        {
            int level = 0;
            var jsonArr = json.ToArray();　　// Using System.Linq;
            string jsonTree = string.Empty;
            for (int i = 0; i < json.Length; i++)
            {
                char c = jsonArr[i];
                if (level > 0 && '\n' == jsonTree.ToArray()[jsonTree.Length - 1])
                {
                    jsonTree += TreeLevel(level);
                }
                switch (c)
                {
                    case '[':
                        jsonTree += c + "\n";
                        level++;
                        break;
                    case ',':
                        jsonTree += c + "\n";
                        break;
                    case ']':
                        jsonTree += "\n";
                        level--;
                        jsonTree += TreeLevel(level);
                        jsonTree += c;
                        break;
                    default:
                        jsonTree += c;
                        break;
                }
            }
            return jsonTree;
        }

        /// <summary>
        /// 树等级
        /// </summary>
        /// <param name="level"></param>
        /// <returns></returns>
        private string TreeLevel(int level)
        {
            string leaf = string.Empty;
            for (int t = 0; t < level; t++)
            {
                leaf += "\t";
            }
            return leaf;
        }
        #endregion
    }
}
